package com.harmontronics.hanma.paas.paas.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmTenantAppManagerAppService;
import com.harmontronics.hanma.paas.paas.dto.output.HmTenantAppManagerResult;
import com.harmontronics.hanma.paas.paas.dto.input.HmTenantAppManagerCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmTenantAppManagerUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmTenantAppManager;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 租户-应用管理员表
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-03-18 14:17:33
 */
@RestController
@RequestMapping("/api/tenant/appmanager")
@Api(tags = {"租户-应用管理员表"})
public class HmTenantAppManagerController {
    @Autowired
    private HmTenantAppManagerAppService hmTenantAppManagerappService;

    /**
     * 列表
     */
//    @GetMapping("/list")
//    @ApiOperation("列表")
//    public ResponseEntity<List<HmTenantAppManagerResult>> list() {
//        List<HmTenantAppManager> hmTenantAppManagerList = hmTenantAppManagerappService.getList();
//
//        return ResponseEntity.ok(Convert.toList(HmTenantAppManagerResult.class, hmTenantAppManagerList));
//    }

    /**
    * 分页列表
    */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmTenantAppManagerResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTenantAppManager> hmTenantAppManagerPageBase = hmTenantAppManagerappService.getList(pageNo, pageSize);
        PageBase<HmTenantAppManagerResult> resultPageBase = PageBase.copy(hmTenantAppManagerPageBase,HmTenantAppManagerResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public ResponseEntity<HmTenantAppManagerResult> info(@PathVariable("id") Long id) {
        HmTenantAppManager hmTenantAppManager =hmTenantAppManagerappService.findById(id);
            HmTenantAppManagerResult result = BeanUtil.toBean(hmTenantAppManager,HmTenantAppManagerResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("新增了应用管理员")
    public ResponseEntity<String> save(@Valid @RequestBody HmTenantAppManagerCreateInput hmTenantAppManagerInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmTenantAppManager hmTenantAppManager =BeanUtil.toBean(hmTenantAppManagerInput,HmTenantAppManager.class);
            hmTenantAppManagerappService.save(hmTenantAppManager);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了应用管理员信息")
    public ResponseEntity<String> update(@Valid @RequestBody HmTenantAppManagerUpdateInput hmTenantAppManagerInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmTenantAppManager hmTenantAppManager =BeanUtil.toBean(hmTenantAppManagerInput,HmTenantAppManager.class);
            hmTenantAppManagerappService.update(hmTenantAppManager);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了应用管理员")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
            hmTenantAppManagerappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
